//
//  BCIndustryScreeningYieldSliderView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/19.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "BCIndustryScreeningYieldSliderView.h"


@protocol BCIndustryScreeningYieldSliderPanViewDelegate <NSObject>

- (void)industryScreeningYieldSliderPanLeft:(BOOL)left percent:(CGFloat)percent;

@end

@interface BCIndustryScreeningYieldSliderPanView : UIView <UIGestureRecognizerDelegate>

@property (nonatomic, strong) UIView *backTintView;
@property (nonatomic, strong) UIView *betweenTintView;
/**
 *tintHeight
 */
@property (nonatomic, assign) CGFloat tintHeight;
//
@property (nonatomic, strong) UIView *leftCircleView;
@property (nonatomic, strong) UIView *rightCircleView;


#pragma mark - gesture
@property (nonatomic, strong) UIPanGestureRecognizer *panGesture;
@property (nonatomic, weak) UIView *panView;

/**
 * BCIndustryScreeningYieldSliderPanViewDelegate
 */
@property (nonatomic, weak) id <BCIndustryScreeningYieldSliderPanViewDelegate> delegate;

@end

@implementation BCIndustryScreeningYieldSliderPanView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupSubviews];
        //
        self.panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureAction:)];
        self.panGesture.delegate = self;
        [self addGestureRecognizer:self.panGesture];
    }
    return self;
}
- (void)setupSubviews {
    self.tintHeight = [self.class tintViewHeight];
    self.backTintView = [UIView new];
    [self addSubview:self.backTintView];
    [self.backTintView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self);
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(self.tintHeight);
    }];
    self.backTintView.layer.cornerRadius = self.tintHeight/2;
    self.backTintView.backgroundColor = [UIColor separatorColor];
    //
    self.betweenTintView = [UIView new];
    self.betweenTintView.backgroundColor = [UIColor darkMainColor];
    [self addSubview:self.betweenTintView];
    //
    CGSize circleSize = [self.class panViewSize];
    self.leftCircleView = [UIView new];
    self.leftCircleView.backgroundColor = [UIColor darkMainColor];
    [self addSubview:self.leftCircleView];
    [self.leftCircleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(0);
        make.centerY.mas_equalTo(self);
        make.size.mas_equalTo(circleSize);
    }];
    //
    self.rightCircleView = [UIView new];
    self.rightCircleView.backgroundColor = [UIColor darkMainColor];
    [self addSubview:self.rightCircleView];
    [self.rightCircleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self).offset(0);
        make.centerY.mas_equalTo(self);
        make.size.mas_equalTo(circleSize);
    }];
    //
    [self.betweenTintView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self);
        make.height.mas_equalTo(self.tintHeight);
        make.left.mas_equalTo(self.leftCircleView.mas_centerX);
        make.right.mas_equalTo(self.rightCircleView.mas_centerX);
    }];
    //
    self.leftCircleView.layer.cornerRadius = self.rightCircleView.layer.cornerRadius = circleSize.width/2;
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if (gestureRecognizer==self.panGesture) {
        //如果交叉以rightCircleView为准
        CGPoint panPoint = [gestureRecognizer locationInView:self];
        if (CGRectContainsPoint(CGRectMake(self.rightCircleView.frame.origin.x-10.0, self.rightCircleView.frame.origin.y-10.0, self.rightCircleView.frame.size.width+20.0, self.rightCircleView.frame.size.width+20.0), panPoint)) {
            self.panView = self.rightCircleView;
            return true;
        }
        if (CGRectContainsPoint(CGRectMake(self.leftCircleView.frame.origin.x-10.0, self.leftCircleView.frame.origin.y-10.0, self.leftCircleView.frame.size.width+20.0, self.leftCircleView.frame.size.width+20.0), panPoint)) {
            self.panView = self.leftCircleView;
            return true;
        }
        return false;
    }
    return true;
}


#pragma mark - panGestureAction
- (void)panGestureAction:(UIPanGestureRecognizer *)pan {
    CGPoint panPoint = [pan locationInView:self];
    if (self.panView==self.leftCircleView) {
        CGFloat offset = MAX(0, MIN(panPoint.x, CGRectGetMinX(self.rightCircleView.frame)-CGRectGetWidth(self.leftCircleView.frame)));
        [self.leftCircleView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self).offset(offset);
        }];
        //计算出距离百分比 中心点为准
        CGFloat percentX = CGRectGetMidX(self.leftCircleView.frame)-CGRectGetWidth(self.leftCircleView.frame)/2;
        CGFloat wholeWidth = CGRectGetWidth(self.frame)-[self.class panViewSize].width;
        CGFloat percent = percentX/wholeWidth;
        [self panLeft:true percent:percent];
    }else if (self.panView==self.rightCircleView) {
        //根据计算左边距离来计算右边
        CGFloat minX = CGRectGetMaxX(self.leftCircleView.frame);
        CGFloat maxX = CGRectGetWidth(self.frame)-CGRectGetWidth(self.rightCircleView.frame);
        CGFloat offsetX = MAX(minX, MIN(maxX, panPoint.x));
        CGFloat offset = maxX-offsetX;
        [self.rightCircleView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self).offset(-offset);
        }];
        //计算出距离百分比 中心点为准
        CGFloat percentX = CGRectGetMidX(self.rightCircleView.frame)-CGRectGetWidth(self.rightCircleView.frame)/2;
        CGFloat wholeWidth = CGRectGetWidth(self.frame)-[self.class panViewSize].width;
        CGFloat percent = percentX/wholeWidth;
        [self panLeft:false percent:percent];
    }
}

- (void)panLeft:(BOOL)left percent:(CGFloat)percent {
    if (self.delegate && [self.delegate respondsToSelector:@selector(industryScreeningYieldSliderPanLeft:percent:)]) {
        [self.delegate industryScreeningYieldSliderPanLeft:left percent:percent];
    }
}


//拖动圆圈视图size
+ (CGSize)panViewSize {
    return CGSizeMake(24.0, 24.0);
}
//tintView的高度
+ (CGFloat)tintViewHeight {
    return 6.0f;
}

@end


//顶部label 的高度
static CGFloat BCIndustryScreeningYieldSliderViewLabelHeight = 20.0f;
//panView 高度
static CGFloat BCIndustryScreeningYieldSliderViewPanViewHeight = 30.0f;

@interface BCIndustryScreeningYieldSliderView () <BCIndustryScreeningYieldSliderPanViewDelegate>

@property (nonatomic, strong) UILabel *minLabel;
@property (nonatomic, strong) UILabel *maxLabel;
@property (nonatomic, strong) UILabel *currentMinLabel;
@property (nonatomic, strong) UILabel *currentMaxLabel;



@property (nonatomic, strong) BCIndustryScreeningYieldSliderPanView *panView;

@end

@implementation BCIndustryScreeningYieldSliderView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupSubviews];
    }
    return self;
}

//
- (void)setupSubviews {
    self.minLabel = [UILabel new];
    [self addSubview:self.minLabel];
    self.maxLabel = [UILabel new];
    [self addSubview:self.maxLabel];
    //
    self.minLabel.textColor = self.maxLabel.textColor = [UIColor qmui_colorWithHexString:@"#666666"];
    self.minLabel.font = self.maxLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightRegular];
    //
    self.maxLabel.textAlignment = NSTextAlignmentRight;
    //
    [self.minLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
        make.height.mas_equalTo(BCIndustryScreeningYieldSliderViewLabelHeight);
    }];
    [self.maxLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
        make.height.mas_equalTo(BCIndustryScreeningYieldSliderViewLabelHeight);
    }];
    //
    self.panView = [BCIndustryScreeningYieldSliderPanView new];
    self.panView.delegate = self;
    [self addSubview:self.panView];
    [self.panView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).offset(BCIndustryScreeningYieldSliderViewLabelHeight);
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(BCIndustryScreeningYieldSliderViewPanViewHeight);
    }];
    //
    self.currentMinLabel = [UILabel new];
    [self.panView addSubview:self.currentMinLabel];
    self.currentMaxLabel = [UILabel new];
    [self.panView addSubview:self.currentMaxLabel];
    //
    self.currentMinLabel.textColor = self.currentMaxLabel.textColor = [UIColor darkMainColor];
    self.currentMinLabel.font = self.currentMaxLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightRegular];
    //
    [self.currentMinLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.panView.mas_top);
        make.height.mas_equalTo(BCIndustryScreeningYieldSliderViewLabelHeight);
        make.left.mas_equalTo(self.panView.leftCircleView.mas_left);
    }];
    [self.currentMaxLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.panView.mas_top);
        make.height.mas_equalTo(BCIndustryScreeningYieldSliderViewLabelHeight);
        make.right.mas_equalTo(self.panView.rightCircleView.mas_right);
    }];
}

- (void)setMinValue:(CGFloat)minValue maxValue:(CGFloat)maxValue {
    //实际判断大小 必须都>=0
    minValue = minValue>=0?minValue:0;
    maxValue = maxValue>=0?maxValue:0;
    //以防最小比最大大
    CGFloat temp = maxValue;
    if (minValue>temp) {
        maxValue = minValue;
        minValue = temp;
    }
    //
    _minValue = minValue;
    _maxValue = maxValue;
    //
    self.minLabel.text = minValue==0?@"0":[NSString stringWithFormat:@"%.0f%%",_minValue*100];
    self.maxLabel.text = maxValue==0?@"0":[NSString stringWithFormat:@"%.0f%%",_maxValue*100];
    //
    self.currentMinValue = _minValue;
    self.currentMaxValue = _maxValue;
}

- (void)setCurrentMinValue:(CGFloat)currentMinValue {
    if (currentMinValue<_minValue) {
        currentMinValue = _minValue;
    }
    if (currentMinValue>_maxValue) {
        currentMinValue = _maxValue;
    }
    _currentMinValue = currentMinValue;
    //
    self.currentMinLabel.text = currentMinValue==0?@"0":[NSString stringWithFormat:@"%.0f%%",currentMinValue*100];
}
- (void)setCurrentMaxValue:(CGFloat)currentMaxValue {
    if (currentMaxValue>_maxValue) {
        currentMaxValue = _maxValue;
    }
    if (currentMaxValue<_minValue) {
        currentMaxValue = _minValue;
    }
    _currentMaxValue = currentMaxValue;
    self.currentMaxLabel.text = currentMaxValue==0?@"0":[NSString stringWithFormat:@"%.0f%%",currentMaxValue*100];
}


#pragma mark - BCIndustryScreeningYieldSliderPanViewDelegate
- (void)industryScreeningYieldSliderPanLeft:(BOOL)left percent:(CGFloat)percent {
    //left 是否左边 否则右边 percent当前滑动值变化比例 (max-min)*percent+min
    CGFloat value = (_maxValue-_minValue)*percent+_minValue;
    if (left) {
        self.currentMinValue = value;
    }else {
        self.currentMaxValue = value;
    }
}


+ (CGFloat)defaultHeight {
    return BCIndustryScreeningYieldSliderViewLabelHeight+BCIndustryScreeningYieldSliderViewPanViewHeight+BCIndustryScreeningYieldSliderViewLabelHeight;
}


@end
